package com.jichaoyun.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jichaoyun.model.entity.LineInfo;
import com.jichaoyun.model.entity.StationInfo;
import com.jichaoyun.common.ex.MyException;
import com.jichaoyun.common.model.Result;
import com.jichaoyun.model.vo.ComParametersVo;
import com.jichaoyun.model.vo.StationParametersVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.jichaoyun.sys.service.LineInfoService;
import com.jichaoyun.sys.service.impl.StationInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljc
 * @since 2023-11-10
 */
@RestController
@Api(tags = {"站点信息接口"})
@RequestMapping("/sys/stationInfo")
public class StationInfoController {

    @Autowired
    private StationInfoServiceImpl stationInfoService;
    @Autowired
    private LineInfoService lineInfoService;

    @ApiOperation("//设置报表结算的小时和日期")
    @PostMapping("/updateAccountDateAndHour")
    public Result<?> updateAccountDateAndHour(Integer id,Integer account_date, Integer account_hour) {
        stationInfoService.updateAccountDateAndHour(id,account_date,account_hour);
        return Result.success("修改成功");
    }

    @ApiOperation("增加站点")
    @PostMapping("/addStation")
    public Result<Void> addStation(@RequestBody StationInfo stationInfo) {//@RequestBody是做json字符串的转换
        this.stationInfoService.addStationInfo(stationInfo);
        return Result.success(null);
    }

    @ApiOperation("参数设置，站点参数，分页查找所有站点信息")
    @GetMapping("/getStationListPage")
    public Result<Map<String,Object>> getStationListPage(@RequestParam(value = "pageNo") Integer  pageNo,
        @RequestParam(value = "pageSize") Integer  pageSize){
        Map<String, Object> data= new HashMap<>();
        IPage<StationInfo> stationInfoIPage = stationInfoService.getAllStationInfo(pageNo,pageSize);
        data.put("stationList",stationInfoIPage.getRecords());
        data.put("pages",stationInfoIPage.getPages());
        data.put("total",stationInfoIPage.getTotal());
        return Result.success(data);
    }
    @ApiOperation(value = "参数设置，站点参数，所有站点信息表，导出到Excel")
    @GetMapping("/DownloadStationInfo")
    public void DownloadStationInfo(HttpServletResponse response) throws IOException {

        stationInfoService.getAllStationExcel(response);
    }

    /**
     * zz
     * @param likeName
     * @return
     */
    @ApiOperation(value = "模糊查找所有站点信息",notes = "LikeName是全模糊查询")
    @GetMapping("/getStationNoNameListByLikeName")
    public Result<List<String>> getStationNoNameListByLikeName(String likeName){

        List<String> infoList = stationInfoService.getStationNoNameListByLikeName(likeName);

        return Result.success(infoList);
    }


    @ApiOperation("参数设置，站点参数，修改站点启用状态")
    @PostMapping("/updateStationStatus")
    public Result<?> updateStationStatus(@RequestParam(value = "stationNo") Integer stationNo){
        return stationInfoService.updateStationStatus(stationNo);
    }

    @ApiOperation("参数设置，站点参数，修改站点信息")
    @PostMapping("/updateStationInfo")
    public Result<Void> updateStationInfo(@RequestBody StationParametersVo stationParameters) {//@RequestBody是做json字符串的转换
        this.stationInfoService.updateStationInfo(stationParameters);
        return Result.success(null);
    }

    @ApiOperation("参数设置，通讯参数，修改站点通讯参数信息")
    @PostMapping("/updateStationComInfo")
    public Result<Void> updateStationComInfo(@RequestBody ComParametersVo comParameters) {//@RequestBody是做json字符串的转换
        this.stationInfoService.updateComParameters(comParameters);
        return Result.success(null);
    }

    @ApiOperation("参数设置，站点参数，删除某站点信息")
    @DeleteMapping("/deleteStation/{stationNo}")
    public Result<?> deleteStationById(@PathVariable("stationNo") Integer stationNo) {
        stationInfoService.removeById(stationNo);
        return Result.success("删除站点成功");
    }

    @ApiOperation("参数设置，线路，查找线路信息")
    @GetMapping("/getLineInfo")
    public Result<?> getLineInfo() {
        return Result.success(lineInfoService.getLineList());
    }

    @ApiOperation("参数设置，线路，更改线路名称")
    @PostMapping("/updateLineAlias")
    public Result<?> updateLineAlias(@RequestBody LineInfo lineInfo) {
        try{
            return lineInfoService.updateLineAlias(lineInfo);
        }catch (MyException e){
            throw e;
        }
    }

    @ApiOperation("参数设置，线路，批量修改线路名称")
    @PostMapping("/updateLineAliasBatch")
    public Result<?> updateLineAliasBatch(@RequestBody List<LineInfo> lineInfos) {
        try{
            return lineInfoService.updateLineAliasBatch(lineInfos);
//            return Result.success("修改成功");
        }catch (MyException e){
            return Result.resultError(e);
        }
    }

    @ApiOperation(value = "参数设置，线路，新增线路",notes = "不用传line_num")
    @PostMapping("/addLineInfo")
    public Result<Object> addLineInfo(@RequestBody LineInfo lineInfo) {
        return lineInfoService.addLineInfo(lineInfo);
    }

    @ApiOperation("根据站号查找站点信息")
    @PostMapping("/selectStationInfoByStation")
    public Result<StationInfo> selectStationInfoByStation(Integer stationNo) {
        return Result.success(stationInfoService.getById(stationNo));
    }



}
